package com.lenovo.lcdm.access.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lenovo.lcdm.access.dto.*;
import com.lenovo.lcdm.access.entity.AccessPolicy;
import com.lenovo.lcdm.access.service.AccessMenuService;
import com.lenovo.lcdm.access.service.AccessPolicyService;
import com.lenovo.lcdm.common.model.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther xujing30
 * @Date 2024/02/01
 * @Description 菜单权限
 */
@RestController
@RequestMapping("/access")
public class AccessController {

    @Autowired
    private AccessMenuService menuService;

    @Autowired
    private AccessPolicyService accessPolicyService;

    @PostMapping("/menu")
    public ResponseVo<Object> saveMenu(@RequestBody MenuDto menuDto) {
        return ResponseVo.success(menuService.saveMenu(menuDto));
    }

    @PutMapping("/menu")
    public ResponseVo<Object> updateMenu(@RequestBody MenuDto menuDto) {
        return ResponseVo.success(menuService.updateMenu(menuDto));
    }

    @DeleteMapping("/menu/{id}")
    public ResponseVo<Object> deleteMenu(@PathVariable Long id) {
        return ResponseVo.success(menuService.deleteMenu(id));
    }

    @GetMapping("/menu/{id}")
    public ResponseVo<MenuDto> getMenuById( @PathVariable Long id) {
        return ResponseVo.success(menuService.getMenuById(id));
    }

    @GetMapping("/menu-tree")
    public ResponseVo<MenuTreeDto> getMenuTree() {
        return ResponseVo.success(menuService.getMenuTree());
    }

    @PostMapping("/policy")
    public ResponseVo<AccessPolicy> saveAccessPolicy(@RequestBody AccessPolicyDto accessPolicyDto) {
        return ResponseVo.success(accessPolicyService.saveAccessPolicy(accessPolicyDto));
    }

    @PutMapping("/policy")
    public ResponseVo<AccessPolicy> updateAccessPolicy(@RequestBody AccessPolicyDto accessPolicyDto) {
        return ResponseVo.success(accessPolicyService.updateAccessPolicy(accessPolicyDto));
    }

    @DeleteMapping("/policy/{id}")
    public ResponseVo<Boolean> deleteAccessPolicy(@PathVariable Long id) {
        return ResponseVo.success(accessPolicyService.deleteAccessPolicy(id));
    }

    @GetMapping("/policy/list")
    public ResponseVo<Page<AccessPolicy>> getAccessPolicy(@RequestParam int pageNo, @RequestParam int pageSize, @RequestParam(required = false) String menuName) {
        Page<AccessPolicy> page = new Page(pageNo, pageSize);
        List<AccessPolicy> data = accessPolicyService.getAccessPolicyPage(page, menuName);
        page.setRecords(data);
        return ResponseVo.success(page);
    }

    @GetMapping("/user-policy")
    public ResponseVo<UserInfoDto> getCurrentUserPolicy() {
        return ResponseVo.success(accessPolicyService.getCurrentUserPolicy());
    }



}
